<?php
/**
  +-----------------------------------------------------
 * 说明 
 * @param 
 * @return 
 * @author vseach@163.com
  +-----------------------------------------------------
 * @example
 */
$ctitle = Subsite::model()->findByPk($model->site_id)->domain;
$this->breadcrumbs = array(
    '子站管理' => array('subsite/admin'),
    '分类列表',
);
Yii::app()->clientScript->registerScript('search', "
$('form.search-form').submit(function(){
	$.fn.yiiGridView.update('SubsiteCategory-grid', {
		data: $(this).serialize()
	});
	return false;
});
$('#delete-button').click(function(){
    var selects = $.fn.yiiGridView.getSelection('SubsiteCategory-grid');
    if(selects==''){
        alert('请选中记录进行操作！');
    }else{
       if(window.confirm('您确定要删除吗？')){
            $.fn.yiiGridView.update('SubsiteCategory-grid', {
		type:'POST',
		url:'" . $this->rewriteCreateUrl('delete') . "',
                                    data:{id:selects},
		success:function(data) {
                    $.fn.yiiGridView.update('SubsiteCategory-grid');
                    //afterDelete(th,true,data);
		},
		error:function() {
                    //afterDelete(th,false);
		}
            });
            return false;
       }
    }
});
");
?>
<div class="box corners shadow">
    <div class="box-header">
        <h2><?php echo $ctitle; ?> 分类设置</h2>
        <div class="box-header-ctrls">	
            <a href="javascript:void(null);" title="" class="close"><!-- --></a>
        </div>
    </div>

    <div class="box-content" id="contacts-1a">
        <div class="inbox-sf">
            <form class="search-form">
                <?php
                $this->Widget('ext.searchField.searchFieldWidget', array(
                    'initText' => '',
                    'titleText' => '输入分类名称查询',
                    'inputName' => 'SubsiteCategory[title]',
                    'inputId' => 'SubsiteCategory_title_search',
                ));
                echo CHtml::dropDownList('SubsiteCategory[product_model]', '', array('' => '--选择模型--') + $this->model_array);
                echo CHtml::dropDownList('SubsiteCategory[status]', '', array('' => '--选择状态--', '0' => '禁用', '1' => '激活'));
                ?>                
                <input type="submit" name="" value="查询" class="inbox-sf-search-btn" />
            </form>
            <a class="button black fr tip" id="delete-button" href="javascript:void(null);" title="选中数据进行删除！">
                <span>批量删除</span>
            </a>
            <a class="button black fr" href="<?php echo $this->rewriteCreateUrl('create', array('site_id' => $model->site_id)) ?>">
                <span>添加分类</span>
            </a>
        </div> 
        <?php
        $model->pid = 0;
        $this->widget('zii.widgets.grid.CGridView', array(
            'id' => 'SubsiteCategory-grid',
            'dataProvider' => $model->search(),
            'cssFile' => '',
            'htmlOptions' => array('class' => ''),
            'template' => '{summary}<div id="loading"></div>{items}{pager}',
            'itemsCssClass' => 'tablebox',
            'pager' => array(//通过pager设置样式   默认为CLinkPager
                'prevPageLabel' => '<<',
                'firstPageLabel' => '首页', //first,last 在默认样式中为{display:none}及不显示，通过样式{display:inline}即可
                'nextPageLabel' => '>>',
                'lastPageLabel' => '末页',
                'header' => '',
                'htmlOptions' => array('class' => 'box-nav')
            ),
            'selectableRows' => 2,
            'columns' => array(
                'id',
                array(
                    'name' => 'title',
                    'type' => 'raw',
                    'value' => 'SubsiteCategory::model()->getCategoryTitle($data)',
                ),
                array(
                    'name' => 'game_id',
                    'value' => 'ProductCategory::model()->getGameNameById($data->game_id)',
                ),
                array(
                    'name' => 'pid',
                    'value' => 'SubsiteCategory::model()->getSortNameById($data->pid)',
                ),
                array(
                    'name' => 'product_model',
                    'value' => 'Yii::app()->controller->model_array[$data->product_model]',
                ),
                array(
                    'name' => 'status',
                    'value' => '$data->status==1?"激活":"禁用"',
                ),
                array(
                    'name' => 'sort',
                    'type' => 'raw',
                    'value' => 'SubsiteCategory::model()->createInput("sort", $data->id, $data->sort)',
                ),
                /*
                  'thum',
                  'page_title',
                  'page_keywords',
                  'page_description',
                  'product_model',
                  'sort',
                  'status',
                 */
            ),
        ));
        ?>         
    </div><!-- END ".box-content" --> 
</div>
<script>
    function dropLowerLevel(obj){
        var parentid = $(obj).attr('rel');
        var topid = $(obj).attr('parentid');
        var addParent = $(obj).parent().parent();
        var getdata =  $(obj).attr('getdata');
        if($(obj).html()=='展开'){
            $(obj).html('收起');
            if(getdata==0){
                var url ="<?php echo $this->createUrl('subsiteCategory/lowerlevel') ?>";
                var categoryString = addParent.find('.title_catagory').html()+'━━';
                $("#SubsiteCategory-grid").addClass("grid-view-loading");
                $.get(url, {'parentid':parentid}, function(data){
                    $.each(data,function(n,o){
                        addTabelTr(addParent,o,categoryString,topid);
                    });
                    $("#SubsiteCategory-grid").removeClass("grid-view-loading");
                }, 'json');
                $(obj).attr('getdata',1);
            }else{
                $(".tr_"+parentid).show();
            }
        }else{
            $(obj).html('展开');
            $(".tr_"+parentid).hide();
        }
    }
    /**
     *添加tr
     **/
    function addTabelTr(obj,o,categoryString,isParent,iscopy){
        var title = '<span class="title_catagory fl" >'+categoryString+"</span><input size='30' type='text' value='"+o.title+"' class='fl' style='margin-right:20px;' field='title' rel='"+o.id+"' onChange='saveChange(this)'>";
        title = title+"<a class='gradient-btn'  rel='"+o.id+"' parent='"+isParent+"' href='javascript:void(0)' onclick='dropLowerLevel(this)'  getdata=0>展开</a>";
        title = title+"<a class='gradient-btn' rel='"+o.id+"' parent='"+isParent+"' href='"+o.vurl+"'>查看</a>";
        title = title+"<a class='gradient-btn' rel='"+o.id+"' parent='"+isParent+"' href='"+o.uurl+"'>编辑</a>";
         title = title+"<a class='gradient-btn' rel='"+o.id+"' parent='"+isParent+"' href='javascript:void(0)' onclick='deleteCategory(this)'>删除</a>";
        var sort = '<input field="sort" rel="'+o.id+'" onblur="saveChange(this)" size="5" type="text" value="'+o.sort+'">';
        //var titlenote = '<input type="text" value="'+o.titlenote+'">';
        var trclsss = " tr_"+o.pid;
        var hClass =  obj.attr('class');
        trclsss = hClass +" "+trclsss;
        var cstring = '<tr class="'+trclsss+'">';
        cstring = cstring+'<td>'+o.id+'</td>';
        cstring = cstring+'<td>'+title+'</td>';
        cstring = cstring+'<td>'+o.game_id+'</td>';
        cstring = cstring+'<td>'+o.pid+'</td>';
        cstring = cstring+'<td>'+o.product_model+'</td>';
        cstring = cstring+'<td>'+o.status+'</td>';
        cstring = cstring+'<td>'+sort+'</td>';
        cstring = cstring+'</tr>';
        if(iscopy){
            obj.before(cstring);
        }else{
            obj.after(cstring);
        }
    }
    /**
     * 保存数据
     **/
    function saveChange(obj){
        var id = $(obj).attr('rel');
        var value = $(obj).val();
        var field = $(obj).attr('field');
        var sdata = {'id':id,field:field,val:value};
       $(obj).addClass("loading");
        var url = "<?php echo $this->createUrl('subsiteCategory/updateChange') ?>";
        $.get(url,sdata, function(data){
            if(data){
                $(obj).removeClass("loading").addClass('ok');
           }else{
                $(obj).removeClass("error");
           }
        });
    }
     /**
     *删除分类
     */
    function deleteCategory(obj){
        if(confirm("您确定要删除这条记录么？")){
            $("#product-category-grid").addClass("grid-view-loading");
            var id = $(obj).attr('rel');
            var url = "<?php echo $this->createUrl('subsiteCategory/delete') ?>";
            $.get(url,{'id':id,'ajax':true},function(data){
                $(obj).parent().parent().remove();
                $("#product-category-grid").removeClass("grid-view-loading");
            });
        }
    }

    function updateField(obj){//更新单个数据内容
        $(obj).addClass("loading");
        $.get("<?php echo $this->createUrl('updateChange'); ?>", {field:$(obj).attr("rel-field"), id:$(obj).attr("rel-id"), val:$(obj).val(), action:"updateField"}, function(data){
            if(data){
                $(obj).removeClass("loading").addClass("ok");
            }else{
                $(obj).removeClass("loading").addClass("error");
            }
        });
    }
</script>